<template>
  <div>
    <el-button @click="_export" type="default">导出excel</el-button>
  </div>
</template>

<script>
  const X = typeof XLSX !== 'undefined' ? XLSX : window.XLSX;

  export default{
    name:'export',
    props:{
      tableData: {
        type: Array,
        default: []
      }
    },
    methods:{
      _export(evt) {
        /* convert state to workbook */
        const ws = X.utils.aoa_to_sheet(this.tableData);
        const wb = X.utils.book_new();
        X.utils.book_append_sheet(wb, ws, "SheetJS");

        /* generate X file */
        const wbout = X.write(wb, {type:"binary", bookType:"xlsx"});
        /* send to client */
        saveAs(new Blob([this.s2ab(wbout)],{type:"application/octet-stream"}), "test.xlsx");
      },
      s2ab(s) {
        const buf = new ArrayBuffer(s.length);
        const view = new Uint8Array(buf);
        for (let i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
        return buf;
      }
    }
  }
</script>
